package com.dlb.andorid;

import androidx.annotation.Nullable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.StrikethroughSpan;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.dlb.andorid.bean.ProductBean;
import com.kwan.xframe.common.image.ImageLoader;
import com.kwan.xframe.common.image.glide.GlideUtil;

import java.util.List;

public class ProductAdapter extends BaseQuickAdapter<ProductBean, BaseViewHolder> {

	ImageLoader mImageLoader = new GlideUtil();

	public ProductAdapter(@Nullable List<ProductBean> data) {
		super(R.layout.list_item_home, data);
	}

	@Override
	protected void convert(BaseViewHolder helper, ProductBean item) {
		mImageLoader.loadUrl(item.getPic_cover(), (ImageView) helper.getView(R.id.iv_icon));
		helper.setText(R.id.tv_name, item.getName());
		helper.setText(R.id.tv_type_name, item.getType_name());
		helper.setText(R.id.tv_price, item.getSale_price() + "");

		helper.setText(R.id.tv_price_wholesale, item.getWholesale_price() + "");
		helper.setVisible(R.id.tv_price_wholesale, App.mUserInfo.getUser_group() == 6);

		String op = item.getOrdinary_price() + "";
		SpannableString sp = new SpannableString(op);
		sp.setSpan(new StrikethroughSpan(), 0, op.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
		((TextView) helper.getView(R.id.tv_price_old)).setText(sp);

		String[] tags = item.getTag_name().split(",");

		if (tags.length == 1) {
			helper.getView(R.id.tv_tag2).setVisibility(View.INVISIBLE);
			if (tags[0] == null || item.getTag_name().isEmpty()) {
				helper.getView(R.id.tv_tag1).setVisibility(View.INVISIBLE);
			} else {
				helper.setText(R.id.tv_tag1, tags[0]);
			}

		} else {

			if (tags[0] == null || item.getTag_name().isEmpty()) {
				helper.getView(R.id.tv_tag1).setVisibility(View.INVISIBLE);
			} else {
				helper.setText(R.id.tv_tag1, tags[0]);
			}

			if (tags[1] == null || item.getTag_name().isEmpty()) {
				helper.getView(R.id.tv_tag2).setVisibility(View.INVISIBLE);
			} else {
				helper.setText(R.id.tv_tag2, tags[1]);
			}
		}

		helper.addOnClickListener(R.id.iv_car);
	}
}
